#ifndef __ENGINE_SERVER_HPP__
#define __ENGINE_SERVER_HPP__

#include "TcpServer/TcpServer.hpp"
#include "ThreadPool/ThreadPool.h"
#include "OnLineFunction/CandidateWords.h"
#include "OnLineFunction/CandidatePages.h"
#include "Cache/Hash_LRU_K.h"

class EngineServer {
    using Callback = TcpConnectionCallback;
public:
    EngineServer(const string & ip, unsigned short port, 
                 size_t threadNum, size_t queSize,
                 size_t cacheCapacity, int cacheSliceNum, int cache_k);
    ~EngineServer();

    // 设置三个回调函数: 分别设置NewConnectionCallback，MessageCallback，CloseCallback
    void setAllCallback(Callback &&cb1, Callback &&cb2, Callback &&cb3);

    // 为关键词推荐器，设置词典库文件、单字索引文件
    void setKeyWordsRecommender(const string &dictFile, const string &indexFile);
    
    // 为网页推荐器，设置倒排索引库文件、网页偏移库文件、停用词文件
    void setPageRecommender(const string& invertedIndexFile, const string& pageOffsetFile, 
                            const string& pageLibFile, const string& stopWordFile);

    // 服务器启动与停止
    void start();
    void stop(); 

    // 根据用户输入内容，分析出相关候选词并返回 (以json格式)
    Json getCandidateWords(const string& input);

    // 根据用户输入内容，分析出相关候选网页并返回 (以json指针格式)
    shared_ptr<Json> getCandidatePages(const string& input);

    // 向线程池添加任务
    void addTask(ElemType && pt);

public:
    HashLruKCaches<string, string> _keywordCache;
    HashLruKCaches<string, string> _pageCache;
    
private:
    TcpServer _server;
    ThreadPool _pool;
    CandidateWords _keyWordsRecommender; 
    CandidatePages _pageRecommender;  
};

#endif // __ENGINE_SERVER_HPP__